/**************************************************************************
 *  创建者：        hualei
 *  开始时间：   2018.06.23
 *  copyright:  (C) 深圳市华友高科有限公司
 *  修改说明：  (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 *
 ***************************************************************************
 *   MOVE_MAG 磁导航指令属性编辑视窗实现
 *
 ***************************************************************************/
#include "../../programView/programview.h"
#include "movemaginstructionview.h"
#include "ui_movemaginstructionview.h"
#include <QIntValidator>

MoveMagInstructionView::MoveMagInstructionView(int styleId,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MoveMagInstructionView)
{
    ui->setupUi(this);
    initialDisplayStyle(styleId);
    ui->VelocityRatio_LineEdit->setValidator(new QIntValidator(0,100,this));
    ui->VelocityRatio_slider->setMaximum(100);
    ui->VelocityRatio_slider->setMinimum(0);
    ui->InputNum_Edit->setValidator(new QIntValidator(1,15,this));
    ui->InputNum_Edit->hide();
    ui->InputNum_label->hide();
}

void MoveMagInstructionView::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        ui->forward_Btn->setGeometry(10,350,70,40);
        ui->save_Btn->setGeometry(360,350,70,40);
        break;
    default:
        break;
    }
}

void MoveMagInstructionView::updateView()
{
    ui->No_Stop_radioButton->setChecked(false);
    ui->RFID_Stop_radioButton->setChecked(false);
    ui->MagWidth_Stop_radioButton->setChecked(false);
    ui->Input_Stop_radioButton->setChecked(false);

    if(stopType == E_MOVE_MAG_NO_STOP)
    {
        ui->No_Stop_radioButton->setChecked(true);
        ui->InputNum_Edit->setEnabled(false);
    }
    else if(stopType == E_MOVE_MAG_INPUT_STOP)
    {
        ui->Input_Stop_radioButton->setChecked(true);
        ui->InputNum_Edit->setEnabled(true);
        ui->InputNum_Edit->setText(QString::number(stopInputNum));
    }
    else if(stopType == E_MOVE_MAG_RFID_STOP)
    {
        ui->RFID_Stop_radioButton->setChecked(true);
        ui->InputNum_Edit->setEnabled(false);
    }
    else if(stopType == E_MOVE_MAG_MAG_WIDTH_STOP)
    {
        ui->MagWidth_Stop_radioButton->setChecked(true);
        ui->InputNum_Edit->setEnabled(false);
    }

    ui->Left_Middle_Right_ComBox->setCurrentIndex(leftMiddleRight);
    ui->Front_Back_ComboBox->setCurrentIndex(frontBack);
    ui->note_lineEdit->setText(annotationStr);
    ui->VelocityRatio_slider->setValue(velocityRatio);
    ui->VelocityRatio_LineEdit->setText(QString("%1").arg(velocityRatio));
    ui->RFID_LineEdit->setText(rfidValue);


}

void MoveMagInstructionView::getInput()
{
    leftMiddleRight = ui->Left_Middle_Right_ComBox->currentIndex();
    frontBack = ui->Front_Back_ComboBox->currentIndex();
    velocityRatio = ui->VelocityRatio_LineEdit->text().toInt();
    rfidValue = ui->RFID_LineEdit->text();
    annotationStr = ui->note_lineEdit->text();
    if(stopType == E_MOVE_MAG_INPUT_STOP)
    {
        stopInputNum = ui->InputNum_Edit->text().toInt();
    }
    else
    {
        stopInputNum = 0;
    }

}
void MoveMagInstructionView::changeLanguage(int language)
{
    if(1 == language)
    {
        translator.load("/agvwork/AgvHmi/baseBag/hmiProject/language/moveMagInstructionView.qm");
        qApp->installTranslator(&translator);
    }
    else
    {
        qApp->removeTranslator(&translator);
    }
    ui->retranslateUi(this);
}

void MoveMagInstructionView::on_save_Btn_clicked()
{
    getInput();
    emit finishEditInstruction();
    emit saveInstruction(instruction);
}

void MoveMagInstructionView::on_forward_Btn_clicked()
{
    this->close();
    programView->instructionListFlag = 0;
    programView->editInstructionViewShowOrHide();
}

void MoveMagInstructionView::on_clearNote_Btn_clicked()
{
    ui->note_lineEdit->clear();
    emit cleanNote();
}

void MoveMagInstructionView::on_VelocityRatio_slider_valueChanged(int value)
{   
    velocityRatio = value;
    ui->VelocityRatio_LineEdit->setText(QString("%1").arg(value));
}

void MoveMagInstructionView::on_VelocityRatio_slider_sliderMoved(int position)
{
    velocityRatio = position;
    ui->VelocityRatio_LineEdit->setText(QString("%1").arg(position));
}

void MoveMagInstructionView::on_VelocityRatio_LineEdit_textEdited(const QString &arg1)
{
    bool ok;
    int value = arg1.toInt(&ok, 10);
    if(ok == true)
    {
        ui->VelocityRatio_slider->setValue(value);
    }
}

void MoveMagInstructionView::on_No_Stop_radioButton_clicked()
{
    stopType = E_MOVE_MAG_NO_STOP;
    ui->InputNum_Edit->setEnabled(false);
    ui->InputNum_Edit->hide();
    ui->InputNum_label->hide();
}

void MoveMagInstructionView::on_RFID_Stop_radioButton_clicked()
{
    stopType = E_MOVE_MAG_RFID_STOP;
    ui->InputNum_Edit->setEnabled(false);
    ui->InputNum_Edit->hide();
    ui->InputNum_label->hide();
}

void MoveMagInstructionView::on_MagWidth_Stop_radioButton_clicked()
{
    stopType = E_MOVE_MAG_MAG_WIDTH_STOP;
    ui->InputNum_Edit->setEnabled(false);
    ui->InputNum_Edit->hide();
    ui->InputNum_label->hide();
}

void MoveMagInstructionView::on_Input_Stop_radioButton_clicked()
{
    stopType = E_MOVE_MAG_INPUT_STOP;
    ui->InputNum_Edit->setEnabled(true);
    ui->InputNum_Edit->show();
    ui->InputNum_label->show();
}
